/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.sheet.lookandfeel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.swing.UIManager;

import twaver.ElementAttribute;
import twaver.Node;
import twaver.table.Category;
import twaver.table.TPropertySheet;
import twaver.table.TPropertySheetPane;
import twaver.table.renderer.BooleanRenderer;
import twaver.table.renderer.ColorRenderer;
import twaver.table.renderer.FontRenderer;
import twaver.table.renderer.NumberRenderer;
import twaver.table.renderer.StringRenderer;
import demo.DemoPane;

public class LookAndFeelDemo extends DemoPane{

    private TPropertySheet sheet = new TPropertySheet();
    private Node node = new Node();

	public LookAndFeelDemo(){
		TPropertySheetPane sheetPane = new TPropertySheetPane(sheet);
		sheetPane.setShowValueInDescription(true);
		sheetPane.getDescriptionPane().setVisible(true);
		
		this.sheet.setElement(node);
		
    	this.add(sheetPane, BorderLayout.CENTER);

		Category.registerCategory("laf.color", "COLOR", true);
		Category.registerCategory("laf.string", "STRING", false);
		Category.registerCategory("laf.integer", "INTEGER", false);
		Category.registerCategory("laf.boolean", "BOOLEAN", false);
		Category.registerCategory("laf.font", "FONT", false);
		
		List attributes = new ArrayList();
		
        Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
        while(keys.hasMoreElements()){
            String key = keys.nextElement().toString();
            Object value = UIManager.get(key);       
            
            String category = null;
            String rendererClass = null;
            if(value instanceof Color){
            	category = "laf.color";
            	rendererClass = ColorRenderer.class.getName() + "@left";
            }
            else if(value instanceof Integer){
            	category = "laf.integer";
            	rendererClass = NumberRenderer.class.getName() + "@left";
            }
            else if(value instanceof String){
            	category = "laf.string";
            	rendererClass = StringRenderer.class.getName() + "@left";
            }
            else if(value instanceof Boolean){
            	category = "laf.boolean";
            	rendererClass = BooleanRenderer.class.getName() + "@left";
            }
            else if(value instanceof Font){
            	category = "laf.font";
            	rendererClass = FontRenderer.class.getName() + "@left";
            }
            
            if(category != null){
            	ElementAttribute attribute = createAttribute(category, key, rendererClass);
            	attributes.add(attribute);
            	node.putClientProperty(key, value);
            }
            
        }
        sheet.registerElementClassAttributes(Node.class, attributes);

    	UIManager.addPropertyChangeListener(new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				if("lookAndFeel".equals(evt.getPropertyName())){					
			        Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
			        while(keys.hasMoreElements()){
			            String key = keys.nextElement().toString();
			            if(node.getClientProperty(key) != null){
			            	node.putClientProperty(key, UIManager.get(key));
			            }
			        }
				}
			}
    	});
        
	}
	
	private ElementAttribute createAttribute(String category, String key, String rendererClass){
    	ElementAttribute attribute = new ElementAttribute();
    	attribute.setCategoryName(category);
    	attribute.setClientPropertyKey(key);
    	attribute.setDisplayName(key);
    	attribute.setEditable(false);
    	attribute.setRendererClass(rendererClass);
    	return attribute;
	}

}